﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;

namespace Outfish {
	
	/// <summary>
	/// Holds stuff to make parsing HTML pages more fluent
	/// </summary>
	static public class FluentExtenders {

		/// <summary>Stores any Response content in Data CONTENT and returns it</summary>
		/// <returns>null if no response content</returns>
		static public string LoadResponseContent( this System.Net.WebException wex, Outfish.ResponseReader detector ){
			string content = null;
			if( wex.Response != null ) {
				content = detector.ReadAllText( (System.Net.HttpWebResponse)wex.Response );
			}
			wex.Data["CONTENT"] = content;
			return content;
		}

		static public NameValueCollection GetFormParameters(this string queryString){
			return FormUrlPostData.GetFormCollection( queryString );
		}
		
		// this for our internal use, internal so it doesn't collide with other types of Join
		static internal string Join(this IEnumerable<string> parts, string glue) {
			var builder = new System.Text.StringBuilder();
			bool addGlue = false;
			foreach(var part in parts){ 
				if( addGlue ){ builder.Append( glue ); } else { addGlue = true; }
				builder.Append( part ); 
			}
			return builder.ToString();
		}
		// joining non-strings
		static internal string JoinObjects<T>(this IEnumerable<T> parts, string glue){
			return parts.Select( o=>o.ToString() ).Join( glue );
		}
		
	}

}
